package cn.fancy.sdk.floatwindow;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import cn.fancy.sdk.JudgeMIUI;

public class MyWindowManager {
	private static FloatBallView floatWindow;

	public static LayoutParams floatWindowParams;

	public static LayoutParams turnWindowParams = null;

	private static WindowManager mWindowManager;

	// 这里进行启动浮标
	public static void createfloatWindow(Context context) {
		Log.e("loginfloat", "loginfloat");
		WindowManager windowManager = getWindowManager(context);
		DisplayMetrics dm = new DisplayMetrics();
		windowManager.getDefaultDisplay().getMetrics(dm);

		if (floatWindow == null) {
			floatWindow = new FloatBallView(context);
			if (floatWindowParams == null || turnWindowParams == null) {
				floatWindowParams = new LayoutParams();

				//boolean isxiaomi = JudgeMIUI.orMIUI();
				if (Build.VERSION.SDK_INT < 26) {
					floatWindowParams.type = LayoutParams.TYPE_SYSTEM_ALERT;
					floatWindowParams.type = LayoutParams.TYPE_APPLICATION_PANEL;
				} else {
					//网上方法1
					floatWindowParams.type = LayoutParams.FIRST_SYSTEM_WINDOW + 38;	//TYPE_APPLICATION_OVERLAY
					
					//网上方法2
					floatWindowParams.type = LayoutParams.TYPE_APPLICATION_PANEL;
					if(context instanceof Activity){
						floatWindowParams.token=((Activity)context).getWindow().getDecorView().getWindowToken();
					}
				}

				floatWindowParams.format = PixelFormat.RGBA_8888;
				floatWindowParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
					| LayoutParams.FLAG_NOT_FOCUSABLE
					| LayoutParams.FLAG_LAYOUT_NO_LIMITS
					| LayoutParams.FLAG_FULLSCREEN
					| LayoutParams.FLAG_LAYOUT_IN_SCREEN;
				floatWindowParams.gravity = Gravity.LEFT | Gravity.TOP;
				floatWindowParams.width = FloatBallView.viewWidth;
				floatWindowParams.height = FloatBallView.viewHeight;

				floatWindowParams.y = judgeFloatBallHeight(context);
				floatWindowParams.x = 40;
				floatWindow.setParams(floatWindowParams);
				windowManager.addView(floatWindow, floatWindowParams);
			} else {
				floatWindow.setParams(turnWindowParams);
				windowManager.addView(floatWindow, turnWindowParams);
			}
		}
	}

	private static int judgeFloatBallHeight(Context context) {
		Resources resources=context.getResources();
		DisplayMetrics displayMetrics=resources.getDisplayMetrics();
		// -40原因是保持悬浮球的中点居中
		int displayheight=displayMetrics.heightPixels/2-40;
		
		//if (QdSdkManager.FLOAT_HEIGHT <= 0 || QdSdkManager.FLOAT_HEIGHT >= 600) {
			return 40;
		//}
		//return QdSdkManager.FLOAT_HEIGHT;
	}

	public static void removefloatWindow(Context context) {
		if (floatWindow != null) {
			WindowManager windowManager = getWindowManager(context);
			windowManager.removeView(floatWindow);
			floatWindow = null;
		}
	}

	static boolean isWindowShowing() {
		return floatWindow != null;
	}

	private static WindowManager getWindowManager(Context context) {
		if (mWindowManager == null) {
			mWindowManager = (WindowManager) context
					.getSystemService(Context.WINDOW_SERVICE);
		}
		return mWindowManager;
	}

}
